package com.whut.gkd.controller;

import com.whut.gkd.bean.ShopInfo;
import com.whut.gkd.bean.User;
import com.whut.gkd.service.IGoodService;
import com.whut.gkd.service.IShopService;
import com.whut.gkd.service.IUserService;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.servlet.ModelAndView;

import javax.servlet.http.HttpSession;

@Controller
@RequestMapping("shop")
public class ShopController {

    @Autowired
    IShopService shopService;

    @Autowired
    IGoodService goodService;

    @Autowired
    private IUserService userService;

    @RequestMapping("showInfo.do")
    public ModelAndView showInfo(HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        User owner = (User) session.getAttribute("user");
        ModelAndView mv = new ModelAndView();
        mv.addObject("shopInfo",shopinfo);
        mv.addObject("owner",owner);
        mv.setViewName("shop-info");
        return mv;
    }

    @RequestMapping("toUpdate.do")
    public ModelAndView toUpdate(HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        ModelAndView mv = new ModelAndView();
        mv.addObject("shopInfo",shopinfo);
        mv.setViewName("shop-update");
        return mv;
    }

    @RequestMapping("update.do")
    public String update(ShopInfo shopInfo, HttpSession session){
        ShopInfo info = (ShopInfo) session.getAttribute("shopinfo");
        info.setShopName(shopInfo.getShopName());
        info.setShopDescription(shopInfo.getShopDescription());
        session.setAttribute("shopinfo",info);
        shopService.update(info);
        return "redirect:showInfo.do";
    }

    @RequestMapping("closeShop.do")
    public String closeShop(HttpSession session){
        ShopInfo shopinfo = (ShopInfo) session.getAttribute("shopinfo");
        shopService.closeShop(shopinfo);
        goodService.closeShop(shopinfo);
        userService.closeShop(shopinfo);
        return "../login";
    }
}
